<?php
/*****************************************
 This file is part of the Dynamo CMS UI Toolkit
 Copyright (c) Dynamo Studios 2008

 *** NOTE ***
 Permission is hereby granted to execute this file as a
 web service under the terms of the license agreement.

 See LICENSE.txt, in the system folder.

 All other rights, other than those expressed in the license
 document, are reserved.

 ****************************************/

/**
 * @name XML Output Base
 * @version 0.1
 * @abstract
 * Provides widgets to output as XML.
 * NEEDS WORK!!
 *
 * @author Sean Micklethwaite
 */


function uiWrapHtml($html){
	return '<![CDATA['.$html.']]>';
}

class cHtmlCode extends cDycmsClientNode
/**
 DESCRIPTION
 	Represents raw HTML source code
**/
{
	protected $mCode;

	function __construct($code)
	{
		$this->mCode = $code;
	}

	function DisplayBody()
	{
		echo(uiWrapHtml($this->mCode));
	}

	function __toString()
	{
		return $this->mCode;
	}
}

class cHtmlWrapper extends cDycmsClientNode
{
	protected $msHeader;
	protected $msFooter;

	function __construct($s_h = "", $s_f = "")
	{
		$this->msHeader = $s_h;
		$this->msFooter = $s_f;
	}

	function DisplayHeader()
	{
		echo(uiWrapHtml($this->msHeader));
	}

	function DisplayFooter()
	{
		echo(uiWrapHtml($this->msFooter));
	}
}


class cHtmlWrap extends cHtmlWrapper
{
	function __construct($s_h, IDycmsClientNode $r_child, $s_f = "")
	{
		parent::__construct($s_h, $s_f);
		$this->AddChild($r_child);
	}
}

class cXmlWrapperTag extends cDycmsClientNode
/**
* @desc
* Wraps its children with an XML tag.
*/
{
	protected $msTag;
	protected $mArgs;
	
	function __construct($sTag, $argv = array()){
		$this->msTag = $sTag;
		$this->mArgs = $argv;
	}
	
	function DisplayHeader(){
		echo '<'.$this->msTag.uiElAttributes($this->mArgs).'>';
	}
	
	function DisplayFooter(){
		echo "</{$this->msTag}>";
	}
}

class cXmlTag extends cDycmsClientNode
/**
* @desc
* A singleton XML tag.
*/
{
	protected $msTag;
	protected $mArgs;
	
	function __construct($sTag, $argv = array()){
		$this->msTag = $sTag;
		$this->mArgs = $argv;
	}
	
	function Display(){
		echo '<'.$this->msTag.uiElAttributes($this->mArgs).' />';
	}
	
	function DisplayHeader(){}
	function DisplayFooter(){}
}

class cUiModule extends cDycmsClientNode
{
	protected $msTitle;
	protected $msId;

	function __construct($s_title, $argv = array())
	{
		$this->msTitle = $s_title;
		$this->msId = isset($argv['id']) ? $argv['id'] : 'ui-module';
	}

	function DisplayHeader() {
		echo "<UiModule id=\"{$this->msId}\"><title>{$this->msTitle}</title>";
	}

	function DisplayFooter() {
		echo '</UiModule>';
	}
}

class cDycmsClientModule extends cUiModule
{

}

class cHtmlBlockElement extends cDycmsClientNode
{
	protected $msElementTag;
	protected $mzAttrib;

	function __construct($s_tag, $argv)
	{
		$this->msElementTag = $s_tag;
		$this->mzAttrib = $argv;
	}

	function DisplayHeader()
	{
		echo "<{$this->msElementTag}";
		if (is_array($this->mzAttrib)) foreach($this->mzAttrib as $name => $val) {
			if($name[0] != '$')
				echo " $name=\"$val\"";
		};
		echo '>';
	}

	function DisplayFooter()
	{
		echo("</{$this->msElementTag}>");
	}
}

class cHtmlHyperlink extends cDycmsClientNode
{
	function __construct($s_uri, $s_caption, $z_arg = array())
	{

	}
}


class cUiTable extends cDycmsClientNode
{
	protected $mCols;

	function __construct($cols = null)
	{
		$this->mCols = $cols;
	}

    function DisplayHeader(){
        echo '<Table>';

        if(is_array($this->mCols)) {
			foreach($this->mCols as $col) {
                if($col instanceof IDycmsDisplayable){
                    echo '<Col>';
                    $col->Display();
                    echo '</Col>';
                } else echo '<Col>'.uiWrapHtml($col).'</Col>';
			};
		}
	}

    function DisplayFooter(){
        echo '</Table>';
    }

	function DisplayBody() {
		foreach($this->mChildren as $field) {
			echo '<Row>';

			$field->Display();

			echo '</Row>';
		}
	}
}

class cUiTableRow extends cDycmsClientNode
{
	function DisplayBody() {
		foreach($this->mChildren as $field) {
			echo '<Field>';
			$field->Display();
			echo '</Field>';
		}
	}
}

class cUiImage extends cDycmsClientNode
{
	protected $msPath;

	function __construct($path){
		$this->msPath = $path;
	}

	function DisplayBody(){

	}
}

class cUiIcon extends cDycmsClientNode
{
	protected $mName;

	function __construct($name) {
		$this->mName = $name;
	}

	function DisplayBody() {
		uiIcon($this->mName);
	}
}

class cUiForm extends cHtmlBlockElement
{
	function __construct($argv = array('action' => '','method' => 'POST')) {
		parent::__construct('form', $argv);
	}

	function DisplayBody() {
		foreach($this->mChildren as $node) {
			if($lbl = $node->GetLabel())
				echo '<span class="ui-formItemLabel">'.$lbl.'</span>';

			$node->Display();
		}
	}
}

class cUiFormItem extends cDycmsClientNode
{
	protected $msLabel;

	function __construct($lbl = '') {
		$this->msLabel = $lbl;
	}

	function GetLabel() {
		return $this->msLabel;
	}
}

class cUiFormData extends cXmlTag
{
    function __construct($name, $value) {
		parent::__construct('FormData', array(
            'type'=>'hidden',
		    'name'=>$name,
            'value'=>$value
		));
	}
}

class cUiCaptcha extends cDycmsClientNode
{
	protected $mId;
	protected $msCode;

	function __construct($id){
		$this->mId = $id;
		$this->msCode = $_SESSION['$uiCaptcha-'.$this->mId];
	}

	function CheckCode($code){
		return (substr($this->msCode, 15, 5) == $code);
	}

	function CreateCode(){
		$this->msCode = $_SESSION['$uiCaptcha-'.$this->mId] = md5(rand(0,0xFFFF));

		$image = ImageCreate(100,20);
		$white = imagecolorallocate($image, 255,255,255);
		$black = imagecolorallocate($image, 0,0,0);
		$grey = imagecolorallocate($image, 204,204,204);

		// Black BG
		imagefill($image, 0, 0, $black);

		// String in white
		imagestring($image, 3, 30, 3, $this->msCode, $white);

		// Lines
		imagerectangle($image,0,0,99,19,$grey);

		// Output the image file
		imagepng($image, '../public/tmp/uiCaptcha-'.substr($this->msCode, 25, 7));
	}

	function Dispose(){
		session_unregister('$uiCaptcha-'.$this->mId);
		unlink('../public/tmp/uiCaptcha-'.substr($this->msCode, 25, 7));
	}

	function DisplayHeader(){

	}

	function DisplayFooter(){
	?><img src="/private/system/public/tmp/uiCaptcha-<?php echo(substr($this->msCode, 25, 7)); ?>" /><?php
	}
}

class cUiButton extends cHtmlBlockElement
{
	function __construct($argv, $text = null, $icon = null) {
		parent::__construct('button', $argv);

		if($icon) {
			$this->AddChild(new cUiIcon($icon));
		};
		if($text) {
			$this->AddChild(new cHtmlCode($text));
		};
	}
}

class cUiButtonSet extends cDycmsClientNode
{
	function __construct($argv, $zButtons){
		foreach($zButtons as $value => $text){
			$argv['value'] = $value;
			$this->AddChild(new cUiButton($argv, $text));
		};		
	}
}

class cUiTextBox extends cDycmsClientNode
{
	protected $mzArg;
	
	function __construct($argv = array(), $lbl = null, $pass = false) {
		if($pass)
			$argv['type'] = 'password';
		else
			$argv['type'] = 'text';

		$this->mzArg = $argv;
	}
}

class cUiCheckbox extends cXmlWrapperTag
{
	function __construct($argv = array(), $lbl = null){
		parent::__construct('Checkbox', $argv);
		if($lbl) $this->AddChild($lbl);
	}
}

class cUiCollection extends cDycmsClientNode
/**
* @desc Represents a set of objects
*/
{
	protected $msName;
	
	function __construct($s_name)
	/**
	* @desc Constructs the object collection
	* @param $s_name	Name of the collection
	*/{
		$this->msName = $s_name;
	}
	
	function DisplayHeader(){
		echo <<<END
<Collection name="{$this->msName}">
END;
	}
	
	function DisplayFooter(){
		echo '</Collection>';
	}
}

class cUiCollectionSubCollection extends cDycmsClientNode
{
	protected $msName;
	protected $msUri;

	function __construct($s_name, $vz_criteria)
	{
		$this->msUri = dyBrowserUrl($vz_criteria);
		$this->msName = $s_name;
	}

	function DisplayHeader()
	{
		echo <<<END
<SubCollection uri="{$this->msUri}" name="{$this->msName}">
END;
	}

	function DisplayFooter()
	{
		echo '</SubCollection>';
	}
}

class cUiCollectionObject extends cDycmsClientNode
{
	protected $msName;
	protected $msUri;

	function __construct($obj)
	{
		$this->msUri = dyViewerUrl($obj->GetClsId(), $obj->GetId());
		$this->msName = $obj->GetSummary();
	}

	function DisplayHeader()
	{
		echo <<<END
<CollectionObject uri="{$this->msUri}" name="{$this->msName}">
END;
	}

	function DisplayFooter()
	{
		echo '</CollectionObject>';
	}
}

class cUiException extends cDycmsClientNode
/**
* @desc Renders exception details
*/
{
	protected $msMessage;
	protected $miCode;

	function __construct(Exception $e)
	{
		$this->msMessage = $e->getMessage();
		$this->miCode = $e->getCode();
	}

	function Display()
	{
?><Exception code="<?php echo $this->miCode; ?>"><?php
		echo $this->msMessage;
?></Exception><?php
	}
}

include_once("page.inc.php");
include_once('admin.inc.php');

?>
